package com.kclmedu.mvc.controller;

import com.kclmedu.mvc.entity.User;
import com.kclmedu.mvc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;

/**************
 * 基于RESTFUL 风格的控制器案例
 * 要点：
 * 动作 + 名词 +  条件
 * 如：
 * GET /api/user/1   返回id为1的用户
 * GET /api/user/     返回用户列表
 * POST /api/user/    通过user对象的JSON 参数创建新的user对象
 * PUT  /api/user/3   更新id为3的发送json格式的用户对象
 * DELETE /api/user/4  删除id为4的用户对象
 * DELETE /api/user/   删除所有用户
 */
@RestController
@RequestMapping(value="/api")
public class HelloWorldRestController {

    @Autowired
    private IUserService userService;

    //--------- 获取所有用户信息 ---------
    @RequestMapping(value="/user",method = RequestMethod.GET)
    public ResponseEntity<List<User>> listUsers() {
        //调用业务方法，查询出所有用户信息
        List<User> users = userService.findAll();
        //判断，如果此集合为空,则告诉前端页面无内容
        if(users.isEmpty()) {
            return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
        }
        //如果不为empty,则返回数据
        return new ResponseEntity<List<User>>(users, HttpStatus.OK);
    }

    //-------- 获取指定id的用户 ----------
    @RequestMapping(value = "/user/{id}",method=RequestMethod.GET)
    public ResponseEntity<User> getUser(@PathVariable("id") Integer id) {
        //调用业务方法
        System.out.println("查询的ID是："+id);
        //
        User user = userService.findById(id);
        //判断
        if(user == null) {
            System.out.printf("没有指定id: %d的记录\n", id);
            return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<User>(user, HttpStatus.OK);
    }

    //--------------- 添加用户信息 ------------------------
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public ResponseEntity<Void> createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
        System.out.println("客户端传输过来的用户信息是："+user);
        //判断用户名是否已经存在
        if(userService.isUserExists(user)) {
            //
            System.out.printf("用户名：%s已经存在了\n", user.getUserName());
            return new ResponseEntity<Void>(HttpStatus.CONFLICT);
        }
        //调用业务方法
        userService.save(user);

        //构建一个响应头
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
        //
        return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
    }

    //----------- 更新用户 ---------------
    @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
    public ResponseEntity<User> updateUser(@PathVariable("id") Integer id, @RequestBody User user) {
        System.out.printf("你要更新的用户对象，其ID是：%d\n", id);
        //
        System.out.printf("传过来的User对象信息有： "+user);
        //根据ID先查询出对象
        User currentUser = userService.findById(id);
        //判断此对象是否存在
        if(currentUser == null) {
            System.out.printf("id为%d 的用户不存在\n", id);
            return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
        }
        //修改对象信息
        currentUser.setRealName(user.getRealName());
        currentUser.setPassword(user.getPassword());
        currentUser.setStatus(user.getStatus());
        currentUser.setUserName(user.getUserName());

        //调用业务方法更新
        userService.update(currentUser);
        //返回
        return new ResponseEntity<>(currentUser, HttpStatus.OK);
    }

    //----------- 删除单个对象 -------------------
    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<User> deleteUser(@PathVariable("id") Integer id) {
        System.out.printf("要删除的用户ID为：%d\n", id);
        //
        User user = userService.findById(id);
        //
        if(user == null) {
            //说明此ID不存在
            System.out.printf("用户ID: %d不存在\n", id);
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        //
        userService.delete(id);
        //删除后返回 无内容
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    //---------- 删除所有用户 ------------
    @RequestMapping(value = "/user", method = RequestMethod.DELETE)
    public ResponseEntity<User> deleteAllUsers() {
        System.out.println("删除所有用户信息");
        //调用业务方法
        userService.deleteAll();
        //返回
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
